
%%% The Ambiguity of Fishing for Fun, October 2022
%%%This function creates T catch-per-trip distributions from data 

function [catchPerTrip,catch_max,catch_min]=CatchTripDistribution(N,M,T,percentile,data)

range=(data(1,1):1:data(size(data,1),1))'; 
catch_min=range(1);

for n=1:size(data,1)  
    if data(n,3)==0
        r(n,:)=data(n,2)*ones(1,T-1);
    else
        pd1=makedist('Normal','mu',data(n,2),'sigma',data(n,3));
        pt=truncate(pd1,0,inf);
        r(n,:)=random(pt,1,T-1);
    end
end

D=horzcat(round(data(:,1)),data(:,2),round(r));
D(1:end,1)=D(1:end,1)+1;
  
for t=1:T
    d=accumarray(D(:,1),D(:,t+1));  
    d=d./sum(d);
    if t==1
    cum_sum=cumsum(d);
    L=length(cum_sum(cum_sum<=percentile));
     if L==0 
     catch_max=0;
     else    
     catch_max=range(L);
     end
    end
   catchPerTrip(:,:,t)=gendist(d(:,1)',M,N)-1;
end

end

